﻿
@{
    ViewData["Title"] = "Index";
}
 
<form class="layui-form" action="">
    <fieldset class="layui-elem-field comfield" style="margin: 0">
        <div style="float:left">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">按钮名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="BtnName" lay-verify="" autocomplete="off" placeholder="输入按钮名称" class="layui-input">
                    </div>
                </div>
            </div>

        </div>

        <div style="float: left">
            <button class="layui-btn layui-btn-sm layui-btn-normal layui-btn-radius" lay-submit=""
                    lay-filter="demo1">
                <span class="layui-icon">
                    &#xe615;
                </span>查询
            </button>
            <button type="reset" class="layui-btn layui-btn-sm layui-btn-normal layui-btn-radius">
                <span class="layui-icon">
                    &#xe639;
                </span>重置
            </button>
        </div>
    </fieldset>
</form>
<div class="layui-btn-group mytool">
    @*<button class="layui-btn layui-btn-normal layui-btn-sm" id="btnadd"><i class="layui-icon">&#xe654;</i>新增</button>
    <button class="layui-btn layui-btn-sm" id="btnedit"><i class="layui-icon">&#xe642;</i>编辑</button>
    <button class="layui-btn layui-btn-danger layui-btn-sm" id="btndel"><i class="layui-icon">&#xe640;</i>删除</button>*@
</div>

<table id="demo" lay-filter="test"></table>
 
<script type="text/html" id="barDemo">
    @*<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"><i class="layui-icon">&#xe615;</i>查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>*@
</script>

<script type="text/javascript">
 
    ; layui.use(['form', 'layedit', 'laydate', 'table', 'layer', 'manageCom'],
        function () {

            var form = layui.form
                , layer = layui.layer
                , laydate = layui.laydate
                , manage = layui.manageCom
                , table = layui.table;

            //日期
            laydate.render({
                elem: '#date'
                , type: 'datetime'
            });
            //根据权限生成按钮
            manage.GenerateBtnByPower();
            //监听提交
            form.on('submit(demo1)', function (data) {
                tableIns.reload({
                    where: data.field
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });

                return false;
            });

            $('.mytool').on('click', "#btn_add",
                function () {
                    layer.open({
                        type: 2,
                        title: '新增资料信息',
                        shadeClose: true,
                        //shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['893px', '600px'],
                        content: '@Url.Action("AddButton")'
                    });
                });
            $('.mytool').on('click', "#btn_edit",
                function () {
                    var checkStatus = table.checkStatus('demo');
                    var checkdata = checkStatus.data;
                    if (checkdata.length != 1) {
                        layer.msg("请选一条数据编辑！");
                        return;
                    }
                    console.info(checkStatus.data[0]);
                    layer.open({
                        type: 2,
                        title: '编辑资料信息',
                        shadeClose: true,
                        //shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['893px', '600px'],
                        content: '@Url.Action("EditButton")/' + checkStatus.data[0].Id
                    });
                });

            $('.mytool').on('click', "#btn_del",
                function () {
                    var checkStatus = table.checkStatus('demo');
                    var checkdata = checkStatus.data;
                    if (checkdata.length == 0) {
                        layer.msg("请选择要删除的数据支持批量删除！");
                        return;
                    }
                    layer.confirm('真的要删??', function (index) {
                        var postdata = [];
                        checkdata.forEach(function (value) {
                            postdata.push(value.Id);
                        });

                        //console.info(postdata);
                        $.ajax({
                            type: "post",
                            url: '@Url.Action("ButtonDelete")',
                            data: { ids: postdata },
                            success: function (data) {
                                console.info(data);
                                if (data.Code === 0) {
                                    layer.msg("删除成功！!");
                                    //obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                    tableIns.reload();
                                } else {
                                    layer.msg(data.Msg);
                                }
                            }
                        });
                        layer.close(index);
                        //向服务端发送删除指令

                    });
                });
            //展示已知数据
            tableIns = table.render({
                elem: '#demo',
                url: '@Url.Action("GetButton")'
                , height: manage.getHeight()
                , cols: [[ //标题栏
                    { checkbox: true, LAY_CHECKED: false } //默认全选
                    , { field: 'Id', title: 'Id', width: 80, sort: true }
                    , { field: 'BtnName', title: '按钮名称', width: 100 }
                    , { field: 'MenuId', title: '所属菜单', width: 100 }
                    //, { field: 'BtnUrl', title: '按钮url', width: 100 }
                    , { field: 'EditType', title: '按钮类型', width: 100 }
                    , { field: 'OrderBy', title: '排序', width: 80 }
                    , { field: 'BtnScript', title: '脚本', width: 120 }
                    //, { field: 'SpName', title: '存储过程', width: 120 }
                    //, { field: 'BtnClass', title: '按钮类名', width: 120 }
                    //, { field: 'BtnIcon', title: '按钮图标', width: 120 }
                    //, { field: 'BtnTip', title: '提示', width: 120 }
                    , { field: 'DisplayCondition', title: '显示条件', width: 120 }
                    , { field: 'IsActive', title: '是否启用', width: 180 }
                    , { field: 'CreateTime', title: '创建时间', width: 180, sort: true }
                    , { fixed: 'right', width: 210, align: 'center', toolbar: '#barDemo' }
                ]]
                //, skin: 'row' //表格风格
                , even: false
                , page: true //是否显示分页
                , limits: [5, 7, 10, 100]
                , limit: 10 //每页默认显示的数量
                , loading: true
            });

            //监听工具条
            table.on('tool(test)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                console.info(data);
                if (layEvent === 'event_detail') { //查看
                    layer.open({
                        type: 2,
                        title: '查看资料信息',
                        shadeClose: true,
                        //shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['893px', '600px'],
                        content: '@Url.Action("EditButton")?Id=' + data.Id + '&type=2'
                    });
                } else if (layEvent === 'event_del') { //删除
                    layer.confirm('真的删除' + data.Id + '这行么', function (index) {
                        var postdata = [];
                        postdata.push(data.Id);

                        $.ajax({
                            type: "post",
                            url: '@Url.Action("ButtonDelete")',
                            data: { ids: postdata },
                            success: function (data) {
                                console.info(data);
                                if (data.Code === 0) {
                                    layer.msg("删除成功！!");
                                    //obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                    tableIns.reload();
                                } else {
                                    layer.msg(data.Msg);
                                }
                            }
                        });
                        layer.close(index);
                        //向服务端发送删除指令

                    });
                } else if (layEvent === 'event_edit') { //编辑
                    layer.open({
                        type: 2,
                        title: '编辑资料信息',
                        shadeClose: true,
                        //shade: false,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['893px', '600px'],
                        content: '@Url.Action("EditButton")/' + data.Id
                    });
                }
            });

        });

</script>